package com.meteor.web.admin;

import com.meteor.pojo.*;
import com.meteor.service.BlogService;
import com.meteor.service.TagService;
import com.meteor.service.TypeService;
import com.meteor.util.BlogBean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/admin")
public class BlogController {

    @Resource
    private BlogService blogService;

    @Resource
    private TypeService typeService;

    @Resource
    private TagService tagService;


    /**
     * 根据标题、分类、是否推荐进行博客查询
     */
    @GetMapping("/queryBlogByTitleTypeRecommend.do")
    public String queryBlogByTitleTypeRecommend(@RequestParam String title, @RequestParam String type, Model model) {
        List<BlogBean> blogs = blogService.queryBlogByTitleTypeRecommend(title, type);
        model.addAttribute("blogs",blogs);
        ResultBean rb = new ResultBean();
        rb.setCode(1);
        model.addAttribute("rb",rb);
        return "admin/blogMagt";
    }

    /**
     * 博客分页查询实现
     */
    @GetMapping("/blogMagt")
    public String queryBlog(Model model) {
        List<BlogBean> blogs = blogService.queryBlog();
        model.addAttribute("blogs",blogs);
        return "admin/blogMagt";
    }


    /**
     * 根据id查询博客
     */
    @GetMapping("/blogMagt/{id}/input")
    public String queryBlogById(@PathVariable Long id,Model model,HttpSession session) {
        Blog blog = blogService.queryBlogById(id);
        List<Type> types = typeService.queryTypes();
        List<Tag> tags = tagService.queryTags();
        model.addAttribute("blog",blog);
        model.addAttribute("types",types);
        model.addAttribute("tags",tags);
        session.setAttribute("id",id);
        return "admin/blogUpdate";
    }
    /**
     * 博客编辑功能实现
     */
    @GetMapping("/updateBlog")
    public String updateBlog(BlogBean blog,HttpSession session) {
        Date currentTime = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(currentTime);
        blog.setUpdateTime(format);
        Long id =(Long) session.getAttribute("id");
        int i = blogService.updateBlog(blog.getContent(), id);
        return "admin/blogMagt";
    }


    /**
     * 根据id删除博客
     */
    @GetMapping("/blogMagt/{id}/delete")
    public String deleteBlogById(@PathVariable Long id,Model model) {
        int i = blogService.deleteBlogById(id);
        if(i > 0) {
            model.addAttribute("message","恭喜您，删除成功！");
        } else {
            model.addAttribute("message","抱歉，删除失败！");
        }
        return "admin/blogMagt";
    }

    /**
     * 博客新增页面跳转
     */
    @GetMapping("/blogSend")
    public String blogSend(Model model) {
        List<BlogBean> blogs = blogService.queryBlog();
        List<Type> types = typeService.queryTypes();
        List<Tag> tags = tagService.queryTags();
        model.addAttribute("blogs",blogs);
        model.addAttribute("types",types);
        model.addAttribute("tags",tags);
        return "admin/blogSend";
    }

    /**
     * 实现博客新增
     */
    @PostMapping("/blogSend-input")
    public String addBlog(BlogBean blog,HttpSession session) {
        Date currentTime = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-HH-dd");
        String format = sdf.format(currentTime);

        User user = (User)session.getAttribute("user");
        blog.setCreateTime(format);
        blog.setUpdateTime(format);
        blog.setViews(1);
        blog.setUserId(user.getId());
        Type type = new Type();
        type.setName(blog.getTypeName());
        typeService.addType(type);
        Type[] type1 = typeService.queryTypeByName(blog.getTypeName());
        blog.setTypeId(type1[type1.length-1].getId());
        int i = blogService.addBlog(blog);
        Tag tag = new Tag();
        tag.setId(type1[type1.length-1].getId());
        tag.setName(blog.getTagName());
        tagService.insert(tag);
        return "admin/blogMagt";
    }

}

















